package com.xcf.controller.business.common;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.entity.Page;
import com.xcf.service.business.common.customs.CustomsService;
import com.xcf.util.Jurisdiction;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/customs")
public class CustomsController extends BaseController {
	@Resource
	private CustomsService customsservice;

	String menuUrl = "customs/declaration"; // 菜单地址(权限用)
	
//报关方式管理列表及查询
	@RequestMapping(value = "/declaration")
	public ModelAndView customs(Page page) {

		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();
			String keywords = pd.getString("CUSTOMS_DECLARATION_MODE"); // 判断报关方式
			if (null != keywords && !"".equals(keywords)) {
				pd.put("CUSTOMS_DECLARATION_MODE", keywords.trim());
			}
			String keywords1 = pd.getString("FAST_SEARCH_CODE");
			if (null != keywords1 && !"".equals(keywords1)) {
				pd.put("FAST_SEARCH_CODE", keywords1.trim());
			}
			String keywords2 = pd.getString("ENABLE");
			if (null != keywords2 && !"".equals(keywords2)) {
				pd.put("ENABLE", keywords2.trim());
			}
			page.setPd(pd);
			List<PageData> list = customsservice.seach(page);
			mv.addObject("pd", pd);
			mv.addObject("list", list);
			mv.setViewName("/business/common/customs/customs_deciaration_mode");
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;

	}
	//保存报关方式管理
	@RequestMapping(value="addcustoms")
	@ResponseBody
	public String addcustoms() {			
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		    pd = this.getPageData();
		    int a=0;
			try {
				customsservice.save(pd);	
			} catch (Exception e) {
				a=-1;
				e.printStackTrace();
			}
			return null;
	}
	//去修改页面
	@RequestMapping(value="edit")
	public ModelAndView editS(){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		  pd = this.getPageData();
		  try {
			pd = customsservice.findByUiId(pd);
			mv.addObject("pd", pd);
			mv.setViewName("/business/common/customs/customs_deciaration_edit");	
		} catch (Exception e) {
			
			e.printStackTrace();
		}						
			
		return mv;
		
		
	}
	//保存
	@RequestMapping(value="editU")
	@ResponseBody
	public String editU(){
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			customsservice.editU(pd);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
		
	}
	
	//删除用户
	 
	@RequestMapping(value="/deleteU")
	@ResponseBody
	public String deleteU(){
		
		
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			customsservice.deleteU(pd);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	
	}
	
	//批量删除
	@RequestMapping(value="/deleteInfo")
	@ResponseBody
	public String deleteinfo(){
		PageData pd = new PageData();
		pd = this.getPageData();
			try {
				
				String data = pd.getString("dataJsonStr");

				JSONArray items = JSONArray.fromObject(data);
				for(int i=0,size=items.size();i<size;i++){
					JSONObject item = items.getJSONObject(i);
					String id = item.getString("ID").trim();
					PageData pdq = new PageData();
					pdq.put("ID", id);
				customsservice.deleteU(pdq);
				}
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		
		return null;
		

		
	}
	
	
}
